package util;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Class for loading web pages from URL
 * 
 * @author ak2765
 * @author Vika
 *
 */
public class WebPageLoader {

	/**
	 * @param urlString
	 * @return
	 */
	public String load(String urlString) {
		URL url;
		InputStream is = null;
		DataInputStream dis;
		String line;

		StringBuilder buff = new StringBuilder();
		
		try {
		    url = new URL(urlString);
		    is = url.openStream();  // throws an IOException
		    dis = new DataInputStream(new BufferedInputStream(is));

		    while ((line = dis.readLine()) != null) {
		        buff.append("\n").append(line);
		    }
		} catch (MalformedURLException mue) {
		     mue.printStackTrace();
		} catch (IOException ioe) {
		     ioe.printStackTrace();
		} finally {
		    try {
		        is.close();
		    } catch (IOException ioEx) {
		    	throw new NestedRuntimeException("Couldn't load web page: " + urlString, ioEx);
		    }
		}
		return buff.toString();

	}
}
